package com.example.auto;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;

import com.example.utils.Logger;

/**
 * 脚本解释器(无需特别的权限)
 */
public abstract class ScriptRunner {
	private static final Logger log = Logger.create("ScriptRunner");
	private static long runtime; //运行时间

	private static int toInt(String s) {
		try {
			return Integer.parseInt(s);
		} catch (Exception e) {
		}
		return 0;
	}

	private static float toFloat(String s) {
		try {
			return Float.parseFloat(s);
		} catch (Exception e) {
		}
		return 0;
	}

	private static interface Execer {
		public void exec(String[] args);
	}

	/**
	 * 触发按键事件（down up）
	 * 
	 * args[0]: time args[2]: keyCode
	 */
	private static class KeyExec implements Execer {

		@Override
		public void exec(String[] args) {
			InputInjector.getInstance().sendKey(toInt(args[0]), toInt(args[2]));
		}
	}

	/**
	 * 触发滑屏事件
	 * 
	 * args[0]: time args[2]: x1 args[3]: y1 args[4]: x2 args[5]: y2 args[6]:
	 * duration
	 */
	private static class SwapExec implements Execer {

		@Override
		public void exec(String[] args) {
			InputInjector.getInstance().sendSwipe(toInt(args[0]),
					toFloat(args[2]), toFloat(args[3]), toFloat(args[4]),
					toFloat(args[5]), toInt(args[6]));
		}
	}

	/**
	 * 触发屏幕点击事件
	 * 
	 * args[0]: time args[2]: x1 args[3]: y1
	 */
	private static class TapExec implements Execer {

		@Override
		public void exec(String[] args) {
			InputInjector.getInstance().sendTap(toInt(args[0]),
					toFloat(args[2]), toFloat(args[3]));
		}
	}

	/**
	 * 返回桌面事件
	 */
	private static class HomeExec implements Execer {

		@Override
		public void exec(String[] args) {
			UserHandlerContext.getInstance().toHome(toInt(args[0]));
		}
	}

	public static enum CMDS {
		tap("tap", new TapExec()), swap("swap", new SwapExec()), key("key",
				new KeyExec()), home("home", new HomeExec());

		private String name;
		private Execer execer;

		private CMDS(String name, Execer execer) {
			this.name = name;
			this.execer = execer;
		}

		public String getName() {
			return name;
		}

		public void exec(String[] args) {
			execer.exec(args);
		}
	}
////////////////////////////////////////////////////////////////////////////////
	/*
	 * #注释  
	 * 1000 tap 32 32 
	 * 3000 swap 100 100 400 800 30
	 */
	private static void parseLine(String line) {
		if (line == null || line.length() == 0)
			return;

		if (line.charAt(0) == '#') { // 注释 忽略
			return;
		} else {
			String[] args = line.split(" ");//TODO 可以尝试用StringTokenizer优化
			if (args != null && args.length > 0) {
				int t = toInt(args[0]);
				
				if (t <= runtime) {//超过时间的不再安排
					String cmd = args[1];
					for (CMDS c : CMDS.values()) {
						if (c.name.equals(cmd)) {
							c.exec(args);
						}
					}

				}
			}
		}

	}

	/**
	 * 运行脚本(注意，因为是静态实现，多次调用将有可能导致运行时间被改写）
	 * @param path 路径
	 * @param runtime 运行时间（毫秒）
	 */
	public static void run(String path, long runtime) {
		log.e("运行脚本: " + path +"   runtime(ms)="+runtime);
		ScriptRunner.runtime = runtime;
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(path));
			String line = null;
//			int i = 0;
			while ((line = br.readLine()) != null) {
//				log.i("" + (i++) + ". " + line);
				parseLine(line);
			}
		} catch (Exception e) {
		} finally {
			try {
				br.close();
			} catch (Exception e) {
			}
		}
	}
}
